import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/*
 * It is a set of bucket:
 * I will implement following functions here:
 * 
 * get(int i) // it will return the bucket at the ith level as a set of buckets
 * 
 * set(i, Set<Bucket> bucket) // it will set the bucket at the ith level
 */
public class BucketSet {
	private Set<Bucket> bucketset = new HashSet<Bucket>();
	
	// get all the buckets available at a given level
	public Set<Bucket> get (int level){
		Set<Bucket> returnset = new HashSet<Bucket>();
		for (Iterator<Bucket> it = bucketset.iterator(); it.hasNext();) {
			Bucket b = it.next();
			if(b.bucket_level == level){
				returnset.add(b);
			}
		}
		return returnset;
	}
	
	// to add a bucket
	// it doesn't allows empty buckets to be added.
	public void add (Bucket b){
		if (b.dest == -1 || b.gw == -1 || b.bucket_level <= 0 || b.nexthop == -1){
			World.println("Illegal add for bucket: "+b, World.debug);
			return;
		}
		bucketset.add(b);
	}
	
	// remove a bucket
	public void remove (Bucket b){
		bucketset.remove(b);
	}

	public Bucket get(long src, long dest) {
		// TODO Auto-generated method stub
		int level = World.delta(src,dest);
		Set<Bucket> options = get(level);
		return World.longestPrefixMatch(options,dest);
	}
}
